package lanou.autohome.findcar.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import lanou.autohome.R;
import lanou.autohome.basetool.NetTool;
import lanou.autohome.findcar.beans.FilterBean;

/**
 * Created by dllo on 16/8/25.
 */
public class FilterAdatper extends BaseAdapter {

    private Context mcontext;
    private FilterBean filterBean;

    public FilterAdatper(Context mcontext) {
        this.mcontext = mcontext;
    }

    public void setFilterBean(FilterBean filterBean) {
        this.filterBean = filterBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return filterBean != null ? filterBean.getResult().getSeries().size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.item_filter_findcar , null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        FilterBean.ResultBean.SeriesBean bean = filterBean.getResult().getSeries().get(position);
        viewHolder.seriesname.setText(bean.getSeriesname());
        viewHolder.pricerange.setText(bean.getPricerange());
//        viewHolder.cornericon.setText(String.valueOf(bean.getCornericon()));
        String rank = filterBean.getResult().getSeries().get(position).getCornericon();
        if(rank.equals("1")){
            viewHolder.cornericon.setVisibility(View.VISIBLE);
            viewHolder.cornericon.setBackgroundResource(R.mipmap.rankfirst);
        }else if(rank.equals("2")){
            viewHolder.cornericon.setVisibility(View.VISIBLE);
            viewHolder.cornericon.setBackgroundResource(R.mipmap.ranksecond);
        }else if(rank.equals("3")){
            viewHolder.cornericon.setVisibility(View.VISIBLE);
            viewHolder.cornericon.setBackgroundResource(R.mipmap.rankthird);
        }else {
            viewHolder.cornericon.setVisibility(View.GONE);
        }

        NetTool netTool = new NetTool();
        netTool.getImage(filterBean.getResult().getSeries().get(position).getThumburl() , viewHolder.thumburl);
        return convertView;
    }

    public class ViewHolder {
        ImageView thumburl;
        TextView seriesname;
        TextView pricerange;
        TextView cornericon;
        public ViewHolder(View convertView){
            thumburl = (ImageView) convertView.findViewById(R.id.thumburl_filter_findcar_item);
            seriesname = (TextView) convertView.findViewById(R.id.seriesname_filter_findcar_item);
            pricerange = (TextView) convertView.findViewById(R.id.pricerange_filter_findcar_item);
            cornericon = (TextView) convertView.findViewById(R.id.cornericon_recommend_speaker_item);
        }
    }

}
